RadioGroup.inherit("Control");

function RadioGroup(groupName, submitState){
	this.submitState=submitState;

	this.group=document.getElementsByName(groupName);
}

RadioGroup.prototype.getEventHandler = function () {
	if (!this.eventHandler){
	    var arr = new Array();
	    for (var i=0; i<this.group.length; i++) {
	        arr.push(this.group[i]);
	    } 
		this.eventHandler=new EventHandler(this, arr);
	}
	return this.eventHandler;
}

RadioGroup.prototype.getCheckedLabel=function () {
	return this.getLabel(this.getCheckedIndex());
}

RadioGroup.prototype.getLabel=function (index) {
	if (index==-1){
		return "";
	}
	if (this.group[index].nextSibling.nodeName.toUpperCase()=="LABEL"){
		return this.group[index].nextSibling.firstChild.data;					
	}
	return "";
}

RadioGroup.prototype.getCheckedIndex=function () {
	for (var i=0; i<this.group.length; i++){
		if (this.group[i].checked){
			return i;					
		}
	}
	return -1;
}

RadioGroup.prototype.getValue=function () {
	if (this.getCheckedIndex()==-1){
		return null;
	}
	return this.group[this.getCheckedIndex()].value;
}

RadioGroup.prototype.setValue=function (value) {
	for (var i=0; i<this.group.length; i++){
		this.group[i].checked = (this.group[i].value == value);
	}
}

RadioGroup.prototype.toString=function () {
	for (var i=0; i<this.group.length; i++){
		if (this.group[i].value==this.submitState){
			return this.getLabel(i);					
		}
	}
	return "";
}

RadioGroup.prototype.setEnabled=function (value) {
	for (var i = 0; i < this.group.length; i++){
		this.group[i].disabled = !value;
	}		
}

RadioGroup.prototype.clear=function () {
	if (this.group.disabled) {
		return;
	}
	for (var i = 1; i < this.group.length; i++){
		this.group[i].checked = false;
	}
	this.group[0].checked = true;
}
